package cn.auto.service;

import java.util.HashMap;
import java.util.List;

import cn.auto.common.model.Round;
import cn.auto.common.model.Runinfo;
import cn.auto.common.model.SuiteCase;
import cn.auto.utility.RedisKit;
import cn.auto.utility.Status;

public class RuninfoService {

	Runinfo runinfo = new Runinfo();
	
	List<Runinfo> infos = null;

	/**
	 * 获取某个round本次需要执行的case
	 * 
	 * @return
	 */
	public HashMap<Object, Object> getCaseWaitRun(String pid, String rid) {
		infos = runinfo.searchCaseFromRound(pid, rid);
		return Status.searchResultCode(infos);
	}

	public HashMap<Object, Object> getRuninfo(String id) {
		infos = runinfo.searchInfo(id);
		return Status.searchResultCode(infos);
	}
	
	public HashMap<Object, Object> getFails(String pid, String rid) {
		infos = runinfo.searchInfo(pid, rid, "failed");
		return Status.searchResultCode(infos);
	}
	
	public HashMap<Object, Object> getFails(String rid) {
		infos = runinfo.searchInfo(rid, "failed");
		return Status.searchResultCode(infos);
	}

	/**
	 * 添加轮次信息的时候，系统调用该方法自动将该轮次测试集中的测试脚本加入到运行信息表中
	 * 
	 * @param sid
	 *            测试集id
	 * @param pid
	 *            项目id
	 * @param rid
	 *            测试轮次id
	 * @return
	 */
	public boolean addRunInfos(String sid, String pid, String rid) {
		boolean isAdd = false;
		SuiteCase sc = new SuiteCase();
		List<SuiteCase> cases = sc.searchSC(sid);
		for (int i = 0; i < cases.size(); i++) {
			isAdd = runinfo.addRunInfo(rid, cases.get(i).get("caseName"), pid);
		}
		return isAdd;
	}

	/**
	 * 在运行表中获取该轮次需要运行的脚本名称，创建任务队列
	 * 
	 * @param pid
	 *            项目id
	 * @param cli
	 *            客户端
	 * @param rid
	 *            测试轮次id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String addTaskQueue(String pid, String cli, String rid) {
		List<Runinfo> tasks = null;
		boolean isCreated = false;

		RedisKit rkit = new RedisKit();
		HashMap<Object, Object> result = getCaseWaitRun(pid, rid);
		if (result.get("status") == Status.SUCCESS) {
			tasks = (List<Runinfo>) result.get("body");
		}
		isCreated=createQueue(rid, tasks);      //round id 作为队列名称

		// create queue then publish message
		if (isCreated == true) {
			rkit.pubMesage(cli, rid);
		}
		
		return Status.booleanResultCode(isCreated);

	}
	
	/**
	 * 创建失败队列，以备rerun
	 * @param pid
	 * @param cli
	 * @param rid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String addFailedQueue(String rid) {
		List<Runinfo> tasks = null;
		boolean isCreated = false;
		Round round= new Round();
		
		List<Round> r=round.searchRound(rid);
		if (r.get(0).get("rerun").equals("1")){                  //如果rerun标志字段是1 则创建失败脚本队列
			HashMap<Object, Object> result = getFails(rid);
			if (result.get("status") == Status.SUCCESS) {
				tasks = (List<Runinfo>) result.get("body");
			}
			isCreated=createQueue(rid, tasks);
		}
		return Status.booleanResultCode(isCreated);
	}
    
	/**
	 * 创建脚本执行队列
	 * @param queueName   队列名称
	 * @param els       要加入的元素，脚本名称
	 * @return
	 */
	public boolean createQueue(String queueName, List<Runinfo> els) {
		boolean stQueue = false;
		RedisKit rkit = new RedisKit();
		for (int i = 0; i < els.size(); i++) {
			String queueElement = els.get(i).get("caseName");
			stQueue = rkit.addQueueEle(queueName, queueElement);
		}
		return stQueue;	
	}
	
//	public void autoCreateFailQueue(String queueName,String pid,String cli) throws InterruptedException {
//		RedisKit rkit = new RedisKit();
//		Long len=1l;
//		int i=30;
//		while(len!=0 || i!=0){
//			len=rkit.getQueueLen(queueName);
//			Thread.sleep(1000);
//			i-=1;
//		}
//		addFailedQueue(queueName);
//	}

}
